import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/infra',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    name: 'dashboard',
    component: Layout,
    meta: {},
    children: [
      {
        path: 'index',
        name: 'dashboardIndex',
        meta: {
          title: '控制台',
          icon: 'tabler:dashboard'
        },
        component: () => import('@/views/dashboard/index.vue')
      }
    ]
  },
  {
    path: '/infra',
    name: 'infra',
    component: Layout,
    redirect: '/infra/servers',
    meta: {
      title: '基础设施管理',
      alwaysShow: true,
      icon: 'streamline:industry-innovation-and-infrastructure'
    },
    children: [
      {
        path: 'servers',
        name: 'servers',
        meta: {
          title: '服务器'
        },
        component: () => import('@/views/infra/Servers.vue')
      },
      {
        path: 'networks',
        name: 'infra-networks',
        meta: {
          title: '网络设备'
        },
        component: () => import('@/views/infra/networks.vue')
      },
      {
        path: 'storages',
        name: 'infra-storages',
        meta: {
          title: '存储管理'
        },
        component: () => import('@/views/infra/storages.vue')
      }
    ]
  },
  {
    path: '/jobs',
    name: 'jobs',
    component: Layout,
    meta: {
      title: '任务调度',
      icon: 'bi:list-task'
    },
    children: [
      {
        path: 'index',
        name: 'jobIndex',
        meta: {
          title: '任务定义'
        },
        component: () => import('@/views/jobs/index.vue')
      },
      {
        path: 'list',
        name: 'jobMonitor',
        meta: {
          title: '任务实例'
        },
        component: () => import('@/views/jobs/monitor.vue')
      }
    ]
  },
  {
    path: '/dataset',
    name: 'dataset',
    component: Layout,
    // redirect: '/dataset/index',
    meta: {},
    children: [
      {
        path: 'index',
        name: 'index',
        meta: {
          title: '数据管理',
          icon: 'material-symbols:database'
        },
        component: () => import('@/views/dataset/index.vue')
      }
    ]
  },
  {
    path: '/images',
    name: 'images',
    component: Layout,
    meta: {},
    children: [
      {
        path: 'index',
        name: 'imagesIndex',
        meta: {
          title: '镜像管理',
          icon: 'mdi:docker'
        },
        component: () => import('@/views/images/index.vue')
      }
    ]
  },
  {
    path: '/monitor',
    name: 'monitor',
    component: Layout,
    meta: {},
    children: [
      {
        path: 'index',
        name: 'monitorIndex',
        meta: {
          title: '监控告警',
          icon: 'mdi:alert-outline'
        },
        component: () => import('@/views/monitor/index.vue')
      }
    ]
  },
  {
    path: '/orgs',
    name: 'orgs',
    component: Layout,
    redirect: '/orgs/orglist',
    meta: {
      title: '组织管理',
      alwaysShow: true,
      icon: 'codicon:organization'
    },
    children: [
      {
        path: 'orglist',
        name: 'orglist',
        meta: {
          title: '组织列表'
        },
        component: () => import('@/views/org/Orgs.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
